package com.atguigu.handler;

import com.atguigu.pojo.Emp;
import com.atguigu.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.List;

/**
 * @author 琉
 * @create 2021-09-10 14:14
 */
//gitee|pull测试
@Controller
public class EmployeeHandler {
    @Autowired
    private EmployeeService employeeService;
    @RequestMapping("/findAll")
    public String findAll(Model model){
        //1. 调用业务层的方法查询所有员工信息
        List<Emp> employeeList = employeeService.findAll();
        //2. 将employeeList存储到请求域
        model.addAttribute("list",employeeList);
        //3. 返回逻辑视图
        return "emp-list";
    }

    //分页
    @RequestMapping("/findByPage/{pageNo}")
    public String getPage(
            @PathVariable("pageNo")Integer pageNo,
            Model model
            ){
        // PageInfo 对象封装了和分页相关的所有信息
        PageInfo<Emp> pageInfo = employeeService.findByPage(pageNo);

        //将pageInfo对象存入模型
        model.addAttribute("pageInfo",pageInfo);
        return "emp-page";
    }

    //删除
    @RequestMapping("removeEmpId")
    public String delete(Integer empId){
         employeeService.removeEmpId(empId);
        return "redirect:/findAll";
    }

    public String save(Emp emp){
        employeeService.saveEmployee(emp);
        return "forward:/findAll";
    }

}
